package paytreasure.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import paytreasure.po.Integral;
import paytreasure.service.IntegralService;
import paytreasure.util.ConstantUtil;
import paytreasure.util.ObjAnalysis;
import paytreasure.vo.ResultVo;

/**
 * 
 * @author xhh
 * @date 2017年7月13日
 *  积分表
 */
@Controller
@RequestMapping(value="/integral")
public class IntegralController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(IntegralController.class);
	@Resource
	private  IntegralService integralService ;
	
	private ResultVo result;

	/**
	 * 
	 * @param 
	 * @return 保存添加积分信息
	 * @return_type: String
	 *
	 */
    @RequestMapping(value = "/insertIntegral.action", method = { RequestMethod.POST},produces = "text/html;charset=UTF-8")
    @ResponseBody
	public String insertIntegral(@RequestBody Map<String, Object> map) throws Exception {

    	Integral inte = new Integral();
    	
    	try {
//    		BeanUtils.populate(inte, map);
    		ObjAnalysis.transMap2Bean(map, inte);
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("保存积分信息失败");
		}
		
    	inte.setScoreId(UUID.randomUUID().toString());
    	inte.setCreateTime(new Date());
		result = integralService.insertIntegral(inte);
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}

    /**
	 * 
	 * @param 
	 * @return  通过id查询，去积分信息页面
	 * @return_type: String
	 *
	 */
	@RequestMapping(value="/toIntegralMessage.action", method={RequestMethod.POST},produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String toIntegralMessage(@RequestBody Map<String, String> map) throws Exception {
		result = integralService.getIntegralById(map.get("scoreId"));
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}

	/**
	 * 
	 * @param id
	 * @return 通过id查询，修改积分信息，保存修改
	 * @throws Exception
	 */
	@RequestMapping(value="/saveEditIntegral.action", method={RequestMethod.POST},produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String saveEditIntegral(@RequestBody HashMap<String, Object> map) throws Exception {
		
		Integral inte = new Integral();
		try {
//    		BeanUtils.populate(inte, map);
    		ObjAnalysis.transMap2Bean(map, inte);
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("保存积分信息失败");
		}
		inte.setUpdateTime(new Date());
		integralService.updateIntegral(inte);
		result = new ResultVo();
		result.isResultSuccess();
		result.setResultObj(inte);
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}
	
		
	 /**
	  * 
	  * @param 
	  * @return
	  *
	  */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

}